package com.jdcjk.util.dtr.zxing;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.jdcjk.project.account.model.entity.AddressInfo;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/12/29 0029.
 * 从数据库拿到地址信息工具类
 */

public class AddressInfoUtis {
    private static final String TAG = AddressInfoUtis.class.getSimpleName();

    /**
     * 地址类型枚举类
     */
    public enum AddressType {
        PROVINCE, CITY, AREA;

        public static AddressType getFromString(String str) {
            if (str.equals(PROVINCE.toString())) {
                return PROVINCE;
            }
            if (str.equals(CITY.toString())) {
                return CITY;
            }
            if (str.equals(AREA.toString())) {
                return AREA;
            }
            return null;
        }
    }

    /**
     * 获得国内所有省的信息list
     *
     * @return
     */
    public static ArrayList<AddressInfo> getProvinceInfo(Context context) {
        // 地址信息集合
        ArrayList<AddressInfo> list = new ArrayList<>();
        // 数据库管理器
        DBManager dbm = new DBManager(context);
        dbm.openDatabase();
        // SQL数据库
        SQLiteDatabase db = dbm.getDatabase();
        try {
            String sql = "select * from province";
            Cursor cursor = db.rawQuery(sql, null);
            cursor.moveToFirst();
            while (!cursor.isLast()) {
                String provinceCode = cursor.getString(cursor.getColumnIndex("code"));
                byte bytes[] = cursor.getBlob(2);
                String name = new String(bytes, "gbk");
                AddressInfo addressInfo = new AddressInfo();
                addressInfo.setName(name);
                addressInfo.setProvinceCode(provinceCode);
                list.add(addressInfo);
                cursor.moveToNext();
            }
            String provinceCode = cursor.getString(cursor.getColumnIndex("code"));
            byte bytes[] = cursor.getBlob(2);
            String name = new String(bytes, "gbk");
            AddressInfo addressInfo = new AddressInfo();
            addressInfo.setName(name);
            addressInfo.setProvinceCode(provinceCode);
            list.add(addressInfo);

        } catch (Exception e) {
        }
        dbm.closeDatabase();
        db.close();
        return list;
    }

    /**
     * 获得指定城市省的lsit
     *
     * @param provinceCode 省代码
     * @return
     */
    public static ArrayList<AddressInfo> getCityInfo(Context context, String provinceCode) {
        // 地址信息集合
        ArrayList<AddressInfo> list = new ArrayList<>();
        // 数据库管理器
        DBManager dbm = new DBManager(context.getApplicationContext());
        dbm.openDatabase();
        // SQL数据库
        SQLiteDatabase db = dbm.getDatabase();
        try {
            String sql = "select * from city where pcode='" + provinceCode + "'";
            Cursor cursor = db.rawQuery(sql, null);
            cursor.moveToFirst();
            while (!cursor.isLast()) {
                String cityCode = cursor.getString(cursor.getColumnIndex("code"));
                byte bytes[] = cursor.getBlob(2);
                String name = new String(bytes, "gbk");
                AddressInfo addressInfo = new AddressInfo();
                addressInfo.setName(name);
                addressInfo.setProvinceCode(cityCode);
                list.add(addressInfo);
                cursor.moveToNext();
            }
            String cityCode = cursor.getString(cursor.getColumnIndex("code"));
            byte bytes[] = cursor.getBlob(2);
            String name = new String(bytes, "gbk");
            AddressInfo addressInfo = new AddressInfo();
            addressInfo.setName(name);
            addressInfo.setProvinceCode(cityCode);
            list.add(addressInfo);

        } catch (Exception e) {
        }
        dbm.closeDatabase();
        db.close();
        return list;
    }

    /**
     * 获得指定城市地图的lsit
     *
     * @param cityCode 城市代码
     * @return
     */
    public static ArrayList<AddressInfo> getAreaInfo(Context context, String cityCode) {
        // 地址信息集合
        ArrayList<AddressInfo> list = new ArrayList<>();
        // 数据库管理器
        DBManager dbm = new DBManager(context.getApplicationContext());
        dbm.openDatabase();
        // SQL数据库
        SQLiteDatabase db = dbm.getDatabase();
        try {
            String sql = "select * from district where pcode='" + cityCode + "'";
            Cursor cursor = db.rawQuery(sql, null);
            cursor.moveToFirst();
            while (!cursor.isLast()) {
                String areaCode = cursor.getString(cursor.getColumnIndex("code"));
                byte bytes[] = cursor.getBlob(2);
                String name = new String(bytes, "gbk");
                AddressInfo addressInfo = new AddressInfo();
                addressInfo.setName(name);
                addressInfo.setProvinceCode(areaCode);
                list.add(addressInfo);
                cursor.moveToNext();
            }
            String areaCode = cursor.getString(cursor.getColumnIndex("code"));
            byte bytes[] = cursor.getBlob(2);
            String name = new String(bytes, "gbk");
            AddressInfo addressInfo = new AddressInfo();
            addressInfo.setName(name);
            addressInfo.setProvinceCode(areaCode);
            list.add(addressInfo);

        } catch (Exception e) {
        }
        dbm.closeDatabase();
        db.close();
        return list;
    }
}
